package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.News;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.downloadaction.Delete;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.downloadaction.DownloadTask;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction.MyDownLoad;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction.NewsContent;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.utils.HttpUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.ViewHolder> {
    public List<News> mNews;
    public Context con;
    public Bitmap bitmap;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    DownloadAdapter.Image image= (DownloadAdapter.Image) msg.obj;
                    if(image.image_id.getTag().equals(image.url)){
                        byte[] image_pic=image.data;
                        Bitmap bitmap=BitmapFactory.decodeByteArray(image_pic,0,image_pic.length);
                        image.image_id.setImageBitmap(bitmap);
                    }
                    break;
            }
        }
    };
    class ViewHolder extends RecyclerView.ViewHolder{
        TextView newsTitle;
        TextView newsContent;
        TextView newsdelete;
        ImageView newsImage;
        LinearLayout linearLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout= (LinearLayout) itemView;
            newsImage=itemView.findViewById(R.id.download_item_image);
            newsTitle=itemView.findViewById(R.id.download_item_title);
            newsContent=itemView.findViewById(R.id.download_item_content);
            newsdelete=itemView.findViewById(R.id.delete);
        }
    }
    public DownloadAdapter(List<News> news,Context mContent){
        this.mNews=news;
        this.con=mContent;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(con==null){
            con=viewGroup.getContext();
        }
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.download_item,viewGroup,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.newsdelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(DownloadTask.NEWS_PATH!=null){
                    Delete delete=new Delete();
                    delete.fileDelete(DownloadTask.NEWS_PATH);
                    Intent intent1=new Intent(con,MyDownLoad.class);
                    DownloadTask.NEWS_PATH=null;
                    con.startActivity(intent1);
                }else {
                    Toast.makeText(con,"删除失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                News news=mNews.get(position);
                Intent intent=new Intent(con,NewsContent.class);
                intent.putExtra(NewsContent.NEWS_ID,news.getNews_id()+"");
                con.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.newsImage.setTag(mNews.get(i).getNew_image());
        ImageBitmap(mNews.get(i).getNew_image(),viewHolder.newsImage);
        viewHolder.newsTitle.setText(mNews.get(i).getNews_title().toString());
        viewHolder.newsContent.setText(mNews.get(i).getNews_content().toString());
        viewHolder.newsdelete.setText("删除");
    }

    @Override
    public int getItemCount() {
        return mNews.size();
    }
    private void ImageBitmap(final String path, final ImageView newsImage) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendOkHttpRequest(path, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        byte[] bytes=response.body().bytes();
                        Message msg=handler.obtainMessage();
                        DownloadAdapter.Image image=new DownloadAdapter.Image();
                        image.image_id=newsImage;
                        image.url=path;
                        image.data=bytes;
                        msg.obj=image;
                        msg.what=1;
                        handler.sendMessage(msg);
                    }
                });
            }
        }).start();
    }
    class Image{
        String url;
        ImageView image_id;
        byte[] data;
    }
}
